<?php

namespace App\Http\Controllers;

use App\Events\BuyPhone;
use App\Models\Phone;
use App\Transformers\PhoneTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PhoneController extends Controller
{
    /**
     * @api {get} /api/phone 可用手机号列表
     * @apiGroup Phone
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "name": "+12744789",
     *                   "status": 1,
     *                   "created_at": "2018-12-20 10:40:53",
     *                   "updated_at": "2018-12-20 10:40:53"
     *               }
     *           ]
     *       }
     */
    public function index(Phone $phone){
        $res = DB::table('phone_settings')->where('status',1)->first();
        if($res){
            $phones = $phone->whereNull('user_id')->where('phone_setting_id',$res->id)->get();
        }
       if(isset($phones) && count($phones)<4 && $res->type != 'sip'){
           event(new BuyPhone());
       }
       if(isset($phones) && !empty($phones) ){
           return $this->response->collection($phones, new PhoneTransformer());
       }else{
           return response()->json([]);
       }
    }
}
